<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <el-tabs>
          <!-- 放置页签 -->
          <el-tab-pane label="角色管理">
            <!-- 新增角色按钮 -->
            <el-row style="height: 60px">
              <el-button icon="el-icon-plus" size="small" type="primary" @click="isShowDialog = true">新增角色</el-button>
            </el-row>
            <!-- 表格 -->
            <el-table border="" :data="roleList">
              <el-table-column prop="name" label="角色名称" width="240" />
              <el-table-column prop="description" label="描述" />
              <el-table-column label="操作">
                <!-- row 这个数据的名字, 是组件库本身决定不能改, 也是自动获取 -->
                <!-- <template v-slot="scope"> -->
                <!-- <template v-slot="{row}"> -->
                <template slot-scope="{row}">
                  <el-button size="small" type="success" @click="showPermission(row.id)">分配权限</el-button>
                  <el-button size="small" type="primary" @click="showEdit(row.id)">编辑</el-button>
                  <el-button size="small" type="danger" @click="delRole(row.id)">删除</el-button>
                </template>
              </el-table-column>
            </el-table>
            <!-- 分页组件 -->
            <el-row type="flex" justify="center" align="middle" style="height: 60px">
              <!-- 分页组件 -->
              <el-pagination :total="total" :page-size="pageConfig.pagesize" layout="prev,pager,next" @current-change="currentChange" />
            </el-row>
          </el-tab-pane>

          <el-tab-pane label="公司信息">
            <el-form label-width="120px" style="margin-top: 50px">
              <el-form-item label="公司名称">
                <el-input v-model="companyInfo.name" disabled style="width: 400px" />
              </el-form-item>
              <el-form-item label="公司地址">
                <el-input v-model="companyInfo.companyAddress" disabled style="width: 400px" />
              </el-form-item>
              <el-form-item label="邮箱">
                <el-input v-model="companyInfo.mailbox" disabled style="width: 400px" />
              </el-form-item>
              <el-form-item label="备注">
                <el-input v-model="companyInfo.remarks" type="textarea" :rows="3" disabled style="width: 400px" />
              </el-form-item>
            </el-form>
          </el-tab-pane>
        </el-tabs>
      </el-card>
      <!-- 角色弹窗 -->
      <el-dialog :title="roleForm.id ? '编辑角色' : '新增角色'" :visible="isShowDialog" @close="btnCancel">
        <el-form ref="roleForm" :model="roleForm" :rules="rules" label-width="120px">
          <el-form-item label="角色名称" prop="name">
            <el-input v-model="roleForm.name" />
          </el-form-item>
          <el-form-item label="角色描述" prop="description">
            <el-input v-model="roleForm.description" />
          </el-form-item>
        </el-form>
        <!-- 底部 -->
        <el-row slot="footer" type="flex" justify="center">
          <el-col :span="6">
            <el-button size="small" @click="btnCancel">取消</el-button>
            <el-button size="small" type="primary" @click="btnOK">确定</el-button>
          </el-col>
        </el-row>
      </el-dialog>

      <!-- 设置角色权限弹窗 -->
      <el-dialog title="分配权限" :visible="isShowPermission" @close="btnPermCancel">
        <!-- data 是数据数组, 套着一堆对象 props 指定要显示的字段,
        default-expand-all 默认展开所有 -->
        <el-checkbox-group v-model="checkList">
          <!-- 本来多个多选框需要自己 v-for 遍历, 但是这里由树形处理了 -->
          <el-tree :data="permList" default-expand-all>
            <template v-slot="scope">
              <el-checkbox :label="scope.data.id">
                {{ scope.data.name }}
              </el-checkbox>
            </template>
          </el-tree>

        </el-checkbox-group>

        <template #footer>
          <el-button size="small" type="primary" @click="btnPermOK">确定</el-button>
          <el-button size="small" @click="btnPermCancel">取消</el-button>
        </template>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { getCompanyInfo, getRoleList, getRoleDetail, editRole, addRole, deleteRole, assignPerm } from '@/api/setting'
import { mapState } from 'vuex'
import { getPermissionList } from '@/api/permission'
import { listToTree } from '@/utils'
export default {
  data() {
    return {
      companyInfo: {},
      roleList: [],
      total: 0,
      isShowDialog: false,
      roleForm: {
        name: '',
        description: ''
      },
      rules: {
        name: [
          { required: true, message: '该项不能为空', trigger: 'blur' }
        ],
        description: [
          { required: true, message: '该项不能为空', trigger: 'blur' }
        ]
      },
      pageConfig: {
        page: 1,
        pagesize: 3
      },
      // 设置权限相关
      isShowPermission: false,
      // 所有权限数组
      permList: [],
      // 被选中的权限数组
      checkList: [],
      // 点击弹窗时储存角色id方便确定的时候复用
      roleId: ''
    }
  },
  computed: {
    ...mapState('user', ['userInfo'])
  },
  async created() {
    // 获取角色列表
    this.getRoleList()
    // 获取公司信息
    this.companyInfo = await getCompanyInfo(this.userInfo.companyId)
    // 获取权限列表
    const res = await getPermissionList()
    console.log('权限列表', res)
    // 权限数据是树形需要递归转换
    // this.permList = res
    this.permList = listToTree(res, '0')
  },
  methods: {
    // 权限设置相关
    async showPermission(id) {
      // 获取角色旧数据, 回显多选框
      const { permIds } = await getRoleDetail(id)
      this.checkList = permIds
      this.roleId = id
      this.isShowPermission = true
    },
    async btnPermOK() {
      await assignPerm({
        id: this.roleId,
        permIds: this.checkList
      })
      this.$message.success('修改成功')
      this.isShowPermission = false
    },
    btnPermCancel() {
      this.checkList = []
      this.isShowPermission = false
    },

    async delRole(id) {
      try {
        // 二次询问
        await this.$confirm('是否确认删除?')
        // 发删除请求
        await deleteRole(id)
        // 提示用户
        this.$message.success('删除成功')
        // 更新页面
        this.getRoleList()
      } catch (error) {
        console.log(error)
      }
    },
    async getRoleList() {
      const { rows, total } = await getRoleList(this.pageConfig)
      this.roleList = rows
      this.total = total
    },
    currentChange(newPage) {
      this.pageConfig.page = newPage
      this.getRoleList()
    },
    async showEdit(id) {
      // 获取数据
      this.roleForm = await getRoleDetail(id)
      // 然后弹出弹窗
      this.isShowDialog = true
    },
    async btnOK() {
      try {
        // 表单验证
        await this.$refs.roleForm.validate()
        // 发送修改请求
        // 根据当前表单是否有id可以反推是否做过数据回显, 也就是到底是新增还是编辑
        this.roleForm.id ? await editRole(this.roleForm) : await addRole(this.roleForm)
        // 关闭弹窗
        this.isShowDialog = false
        // 提示用户
        this.$message.success('操作成功')
        // 更新页面渲染
        this.getRoleList()
      } catch (error) {
        console.log(error)
      }
    },
    btnCancel() {
      // 取消
      // 关闭弹窗
      this.isShowDialog = false
      // 清理报错
      // resetFields函数清理报错非常完美, 但是数据的清理
      // 只能请到第一次显示表单时, 绑定的数据, 清理数据需要手动处理
      this.$refs.roleForm.resetFields()
      this.roleForm = {
        name: '',
        description: ''
      }
    }
  }
}
</script>

<style></style>
